package com.terrygjt.myshop;

import android.content.Context;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

/**
 * Created by terry on 2019-05-20.
 */

public class SqliteThread extends HandlerThread {
    private Handler mHandler,dataHandler;
    private Commodity_Sqlite commodity_Sqlite;
    public SqliteThread(Context context, Handler dataHandler) {
        super("SqliteThread");
        commodity_Sqlite=new Commodity_Sqlite(context);
        this.dataHandler=dataHandler;//用于返回数据
    }
    public void deleteData(String commodityBarcode) {
        Message msg=new Message();
        msg.what=1;
        msg.obj=commodityBarcode;
        mHandler.sendMessage(msg);
    }
    public void updateData(String commodityBarcode, String commodityName, String commodityPrice1, String commoditySpecification1,
                           String commodityPrice2, String commoditySpecification2,String commodityPrice3, String commoditySpecification3) {
        String[] a=new String[8];
        a[0]=commodityBarcode;a[1]=commodityName;a[2]=commodityPrice1;a[3]=commoditySpecification1;
        a[4]=commodityPrice2;a[5]=commoditySpecification2;a[6]=commodityPrice3;a[7]=commoditySpecification3;
        Message msg=new Message();
        msg.what=2;
        msg.obj=a;
        mHandler.sendMessage(msg);
    }

    public void insertData(String commodityBarcode, String commodityName, String commodityPrice1, String commoditySpecification1,
                           String commodityPrice2, String commoditySpecification2,String commodityPrice3, String commoditySpecification3) {
        String[] a=new String[8];
        a[0]=commodityBarcode;a[1]=commodityName;a[2]=commodityPrice1;a[3]=commoditySpecification1;
        a[4]=commodityPrice2;a[5]=commoditySpecification2;a[6]=commodityPrice3;a[7]=commoditySpecification3;
        Message msg=new Message();
        msg.what=3;
        msg.obj=a;
        mHandler.sendMessage(msg);
    }
    /*
    *queryColumnNames表示要查询的列
    * 可以为：commodityBarcode, commodityName, commodityPrice1, commoditySpecification1
    * */
//    public void queryData(String commodityBarcode, @Nullable String[] queryColumnNames) {
//        if(queryColumnNames==null){
//            queryColumnNames=new String[]{"commodityBarcode","commodityName","commodityPrice1","commoditySpecification1",
//                    "commodityPrice2", "commoditySpecification2","commodityPrice3", "commoditySpecification3"};
//        }
//        String[] ob=new String[queryColumnNames.length+1];
//        ob[0]=commodityBarcode;
//        for (int i=0;i<queryColumnNames.length;i++){
//            ob[i+1]=queryColumnNames[i];
//        }
//        Message msg=new Message();
//        msg.what=4;
//        msg.obj=ob;
//        mHandler.sendMessage(msg);
//    }
    public void queryCommodityData(String commodityBarcode) {
        Message msg=new Message();
        msg.what=5;
        msg.obj=commodityBarcode;
        mHandler.sendMessage(msg);
    }
    public void queryAllCommodityAndLog() {
        Message msg=new Message();
        msg.what=6;
        mHandler.sendMessage(msg);
    }
    public void closeSQLiteDatabase(){
        commodity_Sqlite.closeSQLiteDatabase();
    }
    private void dataBack(int mgsWhat,Object o){
        Message message=new Message();
        message.what=mgsWhat;
        message.obj=o;
        dataHandler.sendMessage(message);
    }
    public void initHandler(Looper looper) {
        mHandler = new Handler(looper) {//用于操作数据库
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 1:
                        commodity_Sqlite.deleteData((String)msg.obj);
                        break;
                    case 2:
                        String[] a2=(String[])msg.obj;
                        commodity_Sqlite.updateData(a2[0],a2[1],a2[2],a2[3],a2[4],a2[5],a2[6],a2[7]);
                        break;
                    case 3:
                        String[] a3=(String[])msg.obj;
                        commodity_Sqlite.insertData(a3[0],a3[1],a3[2],a3[3],a3[4],a3[5],a3[6],a3[7]);//终于搞定了，原来是这里出错了，导致方法反复调用自己，怪不得存不进数据
                        break;
                    case 4:
                        String[] a4=(String[])msg.obj;
                        String commodityBarcode=a4[0];
                        String[] queryColumnNames=new String[a4.length-1];
                        for (int i=0;i<queryColumnNames.length;i++){
                            queryColumnNames[i]=a4[i+1];
                        }
                        String[] result4=commodity_Sqlite.queryData(commodityBarcode,queryColumnNames);
                        dataBack(4,result4);
                        break;
                    case 5:
                        String a5=(String)msg.obj;
                        String[] result5=commodity_Sqlite.queryData(a5,null);
                        dataBack(5,result5);
                        break;
                    case 6:
                        commodity_Sqlite.queryAllDataAndLog(dataHandler);
                        break;
                }
            }
        };
    }
}
